﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace ProjectView.Windows
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ClassThresholdWindow : Window
    {
        public static readonly DependencyProperty ClassNumProperty = DependencyProperty.Register(
            "ClassNum",
            typeof(List<double>),
            typeof(ClassThresholdWindow),
            new UIPropertyMetadata(null, OnClassNumChanged)
        );

        public static readonly DependencyProperty ClassThresholdProperty = DependencyProperty.Register(
            "ClassThreshold", 
            typeof(double), 
            typeof(ClassThresholdWindow), 
            new UIPropertyMetadata(0.0)
        );

        public List<double> ClassNum
        {
            get
            {
                return (List<double>)GetValue(ClassNumProperty);
            }
            set
            {
                SetValue(ClassNumProperty, value);
            }
        }

        public double ClassThreshold
        {
            get
            {
                return (double)GetValue(ClassThresholdProperty);
            }
            set
            {
                SetValue(ClassThresholdProperty, value);
            }
        }

        private static void OnClassNumChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            ClassThresholdWindow classThresholdWindow = sender as ClassThresholdWindow;

            if (classThresholdWindow != null)
            {
                classThresholdWindow.RefreshClassNum();
            }
        }

        public ClassThresholdWindow()
        {
            InitializeComponent();
            sp.Visibility = Visibility.Collapsed;
        }

        private void RefreshClassNum()
        {
            sp.Visibility = ((ClassNum == null || ClassNum.Count == 0) ? Visibility.Collapsed : Visibility.Visible);
            if (sp.Visibility == Visibility.Collapsed)
            {
                return;
            }
            sp.Children.Clear();
            slider.Minimum = ClassNum[0];
            slider.Maximum = ClassNum[ClassNum.Count - 1];
            for (int i = 1; i < ClassNum.Count; i++)
            {
                int num = ClassNum.Count - i;
                TextBlock textBlock = new TextBlock();
                textBlock.TextAlignment = TextAlignment.Center;
                textBlock.VerticalAlignment = VerticalAlignment.Center;
                textBlock.Text = num.ToString();
                textBlock.Width = (sp.Width - 1.0 * (double)(ClassNum.Count - 2)) * (ClassNum[i] - ClassNum[i - 1]) / (slider.Maximum - slider.Minimum);
                sp.Children.Add(textBlock);
                if (i != ClassNum.Count - 1)
                {
                    Rectangle rectangle = new Rectangle();
                    rectangle.Width = 1.0;
                    rectangle.Height = 16.0;
                    rectangle.VerticalAlignment = VerticalAlignment.Center;
                    rectangle.Stroke = new SolidColorBrush(Colors.Black);
                    sp.Children.Add(rectangle);
                }
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            base.DialogResult = true;
            Close();
        }
    }
}
